class Activity < ActiveRecord::Base
  belongs_to :phase
  belongs_to :project
  has_many :tasks, :dependent=>:destroy
  has_many :workunits
  has_many :collections

  #Finds total cost for activity.
  def total_working_cost
    total_cost = tasks.collect { |a| a.total_working_cost}.sum
    total_cost
  end

  #Finds total working hours for activity .
  def total_working_hours
    total_hours = tasks.collect { |a| a.total_working_hours}.sum
    total_hours
  end

  #Finds total time spent in revision for this activity.
  def total_time_spent_for_revision
    total_hours = 0
    tasks.each { |task|
      task.workunits.each { |workunit|
        total_hours += workunit.amount if workunit.at_status ==
          "In Revision" unless workunit.blank?
      }
    }
    return total_hours
  end

  #Finds total cost spent in revision for this activity.
  def total_cost_for_revision
    total_cost = 0
    tasks.each { |task|
      task.workunits.each { |workunit|
        total_cost += workunit.cost if workunit.at_status ==
          "In Revision" unless workunit.blank?
      }
    }
    total_cost
  end

  #Finds total money collected in project currency or TL for this activity.
  def collection_amount_total (in_project_currency=false)
    sum=0
    collections.each { |cllctn|
      if in_project_currency #calculate amount in project currency
        unless cllctn.currency==project.currency #collection in TL
          sum+= cllctn.amount/cllctn.currency_rate unless cllctn.currency_rate <=0
        else #collection in project currency
          sum+=cllctn.amount 
        end
      else #calculate amount in project currency
        amount = cllctn.amount * ( ["USD","EUR"].include?(cllctn.currency) ? cllctn.currency_rate : 1)
        sum += amount
      end
    } if collections.length>0
    sum
  end

  #Finds percent of money collection for this activity.
  def collection_percent_total
    sum=0
    collections.each { |cllctn|      sum=sum+cllctn.percent    } if collections.length>0
    sum
  end

  #Finds expected cost of activity in project (in project currency).
  def estimated_cost_in_project
    est_cost=0
    weight= self.weight.to_f
    fee = project.fee.to_f
    est_cost = ((fee/100)*weight).to_f unless weight<=0
    est_cost
  end

  #Returns current profit-loss status.
  def profit_loss
    collection_amount_total - total_working_cost
  end

  #Finds how much this activity is worked for through whole project.
  def percent_in_total_working_hours
    percent = 0
    percent = (total_working_hours*100 / project.total_working_hours.to_i)  if project.total_working_hours > 0
    percent
  end

  #Finds how much this activity is worked for through whole project.
  def uncollected_amount
    estimated_cost_in_project - collection_amount_total(true)
  end

  #Finds how much this activity is worked for (in revision phase) through whole phase.
  def revision_percent_in_total_working_hours
    percent = 0
    percent = (total_time_spent_for_revision * 100 / total_working_hours)  if total_working_hours > 0
    percent
  end

end